package com.graducationdesign.springboot.config;

import com.graducationdesign.springboot.component.MyLocaleResolver;
import com.graducationdesign.springboot.interceptor.LoginHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Mr.zhneg
 * @create 2019-10-28-11:42
 */
@Configuration
public class MySpringMVCConfigurer implements WebMvcConfigurer {

   /** 接口 WebMvcConfigurer
    描述
    定义回调方法以自定义通过启用的Spring MVC的基于Java的配置@EnableWebMvc。*/
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {

        return new WebMvcConfigurer(){
            //添加视图控制

            /**
             * 1：ViewControllerRegistry类
             * 协助注册预先配置有状态码和/或视图的简单自动化控制器。
             *
             *  1: ViewControllerRegistration	addViewController(String urlPath)
             将视图控制器映射到给定的URL路径（或模式），以使用预先配置的状态代码和视图来呈现响应。

             2：类ViewControllerRegistration
             协助注册单个视图控制器。

             void	setViewName(String viewName)
             设置视图名称以返回。
             */
            //发送/请求回来到login页面

            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/adminlogin").setViewName("Administrator/main/login");
                registry.addViewController("/studentlogin").setViewName("Student/main/login");
                registry.addViewController("/teacherlogin").setViewName("Teacher/main/login");

                registry.addViewController("/indexa.html").setViewName("Administrator/main/login");
                registry.addViewController("/indexs.html").setViewName("Student/main/login");
                registry.addViewController("/indext.html").setViewName("Teacher/main/login");

                registry.addViewController("/maina.html").setViewName("Administrator/main/indexs");
                registry.addViewController("/mains.html").setViewName("Studnet/main/index");
                registry.addViewController("/maint.html").setViewName("Teacher/main/index");


            }

          @Override
            /**
             * 1：org.springframework.web.servlet.config.annotation
             类InterceptorRegistry
             帮助配置映射的拦截器列表。

             ---InterceptorRegistration	addInterceptor(HandlerInterceptor interceptor)
             添加提供的HandlerInterceptor.
             */
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(new LoginHandlerInterceptor())

                        //指定要拦截的请求 /** 表示拦截所有请求
                        .addPathPatterns("/**")

                       //排除不需要拦截的请求路径
                       .excludePathPatterns("/adminlogin","/indexa.html","/studentlogin","/teacherlogin","/indexs.html","/indext.html","/loginteacher","/loginstudent","/loginadmin")

                      //springboot2+之后需要将静态资源文件的访问路径 也排除
                      .excludePathPatterns("/css/*", "/img/*","/js/*");
            }

        };


    }



    //区域解析器
    @Bean
    public LocaleResolver localeResolver(){

        return new MyLocaleResolver();
    }
}
